package com.jit.monitor.ui.adapter.part;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jit.baselib.util.image.GlideLoaderUtils;
import com.jit.monitor.R;
import com.jit.monitor.commom.AppConstant;
import com.jit.monitor.model.CheckDataManager;
import com.jit.monitor.model.response.MainResponse;
import com.kyleduo.switchbutton.SwitchButton;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * @author crazyZhangxl on 2018/12/27.
 * Describe: 主界面的adapter
 */
public class MainListPartAdapter extends RecyclerView.Adapter<MainListPartAdapter.ViewHolder>{
    public static final int JUST_REFRESH_NAME = 0;
    public static final int JUST_REFRESH_SWITCH_BUTTON = 1;
    public static final int JUST_REFRESH_LIGHT = 2;

    private Context mContext;
    private List<MainResponse> mMainResponseList;
    private MainListListener mMainListListener;
    private boolean onBind;
    private SparseArray<AnimationDrawable> mAnimationDrawableSparseArray;


    public MainListPartAdapter(Context context, List<MainResponse> mainResponseList, MainListListener mainListListener) {
        mContext = context;
        mMainResponseList = mainResponseList;
        mMainListListener = mainListListener;
        mAnimationDrawableSparseArray = new SparseArray<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_main_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Log.e("灯", "构造函数 "+position);
        MainResponse item = mMainResponseList.get(position);
        // 加载图片
        if (item.getFrameInfo() != null){
            holder.mIvFrame.setVisibility(View.VISIBLE);
            holder.tvTimeNow.setVisibility(View.VISIBLE);
            holder.tvNoFrame.setVisibility(View.GONE);
            holder.tvNumber.setVisibility(View.VISIBLE);
            if (item.getFrameInfo().getImage()!= null) {
                GlideLoaderUtils.display(mContext, holder.mIvFrame, item.getFrameInfo().getImage());
                // 设置点击事件-----
                holder.mIvFrame.setOnClickListener(new PicSelect(item.getFrameInfo().getImage()));
                String id = item.getFrameInfo().getId();
                holder.tvTimeNow.setText(item.getFrameInfo().getCreateTime());
                holder.tvNumber.setText("帧信息编号:"+id.substring(0,8));
                holder.tvNumber.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mMainListListener != null){
                            mMainListListener.onNumberClick(id);
                        }
                    }
                });
            }
        }else {
            holder.mIvFrame.setVisibility(View.GONE);
            holder.tvTimeNow.setVisibility(View.GONE);
            holder.tvNoFrame.setVisibility(View.VISIBLE);
            holder.tvNumber.setVisibility(View.GONE);
        }

        if (item.getCamera() != null) {
            holder.tvState.setVisibility(View.VISIBLE);
            holder.tvCameraName.setVisibility(View.VISIBLE);
            holder.tvIp.setVisibility(View.VISIBLE);
            holder.mRlImage.setVisibility(View.VISIBLE);
            holder.tvNoCamera.setVisibility(View.GONE);

            onBind = true;
            holder.sbPumpState.setCheckedImmediately(AppConstant.NOTIFY_WITH_LED_ON.equals(item.getCamera().getEnableNotification()));
            onBind = false;
            holder.sbPumpState.setOnCheckedChangeListener(new MyMaxCheckListener(position,item.getCamera().getId(),item.getCamera().getName()));

            AnimationDrawable animationDrawable = mAnimationDrawableSparseArray.get(position);
            if (null == animationDrawable){
                animationDrawable = (AnimationDrawable) holder.ivLight.getDrawable();
                mAnimationDrawableSparseArray.put(position,animationDrawable);
            }
            // 获得LED状态 记得这里用于测试的
            if (AppConstant.NOTIFY_WITH_LED_ON.equals(item.getCamera().getLedFlag())){
                holder.rlWarnning.setVisibility(View.VISIBLE);
                if (!animationDrawable.isRunning()) {
                    animationDrawable.start();
                }
            }else {
                holder.rlWarnning.setVisibility(View.GONE);
                if (animationDrawable.isRunning()){
                    animationDrawable.stop();
                }
                mAnimationDrawableSparseArray.delete(position);
            }

            // 设置了解了的点击事件咯----
            holder.tvISee.setOnClickListener(new MyISeeClickListener(position,item.getCamera().getId()));

            holder.ivHistory.setVisibility(View.VISIBLE);
            holder.ivHistory.setOnClickListener(new HistoryClickListener(item.getCamera().getId(),
                    item.getCamera().getName()));

            String state = item.getCamera().getState();
            if (null == state){
                holder.tvState.setText("无状态");
            }else {
                if ("stop".equals(state)){
                    holder.tvState.setText("停止");
                }else {
                    holder.tvState.setText("运行");
                }
            }
            if (null == item.getCamera().getName()){
                holder.tvCameraName.setText("暂未录入摄像头名称");
            }else {
                holder.tvCameraName.setText("摄像头名称:" + item.getCamera().getName());
            }
            if (null == item.getCamera().getIp()){
                holder.tvIp.setText("摄像头地址为空");
            }else {
                holder.tvIp.setText("摄像头地址:"+item.getCamera().getIp());
            }
            holder.mRlImage.setOnClickListener(new EditSelected(position,item.getCamera().getName(),item.getCamera().getId()));
        }else {
            holder.tvState.setVisibility(View.GONE);
            holder.tvCameraName.setText(View.GONE);
            holder.tvIp.setVisibility(View.GONE);
            holder.mRlImage.setVisibility(View.GONE);
            holder.tvNoCamera.setVisibility(View.VISIBLE);
            holder.ivHistory.setVisibility(View.GONE);
        }

        if (item.getObjTypeOutList() != null && item.getObjTypeOutList().size() != 0){
            holder.tvNoInfo.setVisibility(View.GONE);
            List<MainResponse.ObjTypeOutListBean> objTypeOutList = item.getObjTypeOutList();
            List<String> mValues = new ArrayList<>();
            mValues.clear();
            for (int i=0;i<objTypeOutList.size();i++){
                mValues.add(CheckDataManager.getCheckChineseName(objTypeOutList.get(i).getObjType())+" "+objTypeOutList.get(i).getTypeNum());
            }
            holder.mFlowLayout.setAdapter(new TagAdapter<String>(mValues) {
                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    TextView tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_tv,
                            parent, false);
                    GradientDrawable mGradientDrawable = new GradientDrawable();
                    mGradientDrawable.setCornerRadius(10);
                    mGradientDrawable.setStroke(1,CheckDataManager.getColor(position));
                    tv.setBackground(mGradientDrawable);
                    tv.setText(s);
                    return tv;
                }
            });

            holder.mFlowLayout.setOnTagClickListener((view, position1, parent) -> {
                if (mMainListListener != null) {
                    // 设置点击时间 -------------
                    mMainListListener.onDetSelected(item.getFrameInfo().getId(), item.getObjTypeOutList().get(position1).getObjType());
                }
                return true;
            });
        }else {
            //
            holder.tvNoInfo.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        Log.e("刷新", "onBindViewHolder:三餐构造 "+position );
        if (payloads.isEmpty()){
            onBindViewHolder(holder,position);
        }else {
            MainResponse item = mMainResponseList.get(position);
            int  flag = (int)payloads.get(0);
            if (JUST_REFRESH_NAME == flag){
                holder.tvCameraName.setText(item.getCamera().getName());
            }else if (JUST_REFRESH_SWITCH_BUTTON == flag){
                onBind = true;
                holder.sbPumpState.setCheckedImmediately(AppConstant.NOTIFY_WITH_LED_ON.equals(item.getCamera().getEnableNotification()));
                onBind = false;
            }else if (JUST_REFRESH_LIGHT == flag){
                // 进行开启以及关闭等
                AnimationDrawable animationDrawable = mAnimationDrawableSparseArray.get(position);
                if (null == animationDrawable){
                    animationDrawable = (AnimationDrawable) holder.ivLight.getDrawable();
                    mAnimationDrawableSparseArray.put(position,animationDrawable);
                }
                // 获得LED状态
                if (AppConstant.NOTIFY_WITH_LED_ON.equals(item.getCamera().getLedFlag())){
                    if (!animationDrawable.isRunning()) {
                        animationDrawable.start();
                    }
                    holder.rlWarnning.setVisibility(View.VISIBLE);
                }else {
                    Log.e("light", "onBindViewHolder:执行了的   "+position);
                    if (animationDrawable.isRunning()){
                        animationDrawable.stop();
                    }
                    holder.rlWarnning.setVisibility(View.GONE);
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return mMainResponseList.size();
    }

    private class PicSelect implements View.OnClickListener{
        private String url;

        public PicSelect( String url) {
            this.url = url;
        }

        @Override
        public void onClick(View v) {
            if (mMainListListener != null){
                // 设置图片选择
                mMainListListener.onImageSelected((ImageView)v,url);
            }
        }
    }

    public class MyMaxCheckListener implements CompoundButton.OnCheckedChangeListener{
        private int position;
        private String cameraName,cameraID;

        public MyMaxCheckListener(int position,String cameraID,String cameraName) {
            this.position = position;
            this.cameraName = cameraName;
            this.cameraID = cameraID;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mMainListListener != null && !onBind){
                mMainListListener.onSwitchButtonOnCheck(position,isChecked,cameraID,cameraName);
            }
        }
    }

    public class MyISeeClickListener implements View.OnClickListener{
        private int position;
        private String cameraID;

        public MyISeeClickListener(int position, String cameraID) {
            this.position = position;
            this.cameraID = cameraID;
        }

        @Override
        public void onClick(View v) {
            if (null != mMainListListener){
                mMainListListener.onISeeClicked(position,cameraID);
            }
        }
    }

    private class EditSelected implements View.OnClickListener{
        private int index;
        private String frameID;
        private String text;

        public EditSelected(int index, String text,String frameID) {
            this.index = index;
            this.text = text;
            this.frameID = frameID;
        }

        @Override
        public void onClick(View v) {
            if (mMainListListener != null){
                mMainListListener.onEditSelected(index,text,frameID);
            }
        }
    }

    private class HistoryClickListener implements View.OnClickListener{
        private String cameraID;
        private String cameraName;

        public HistoryClickListener(String cameraID, String cameraName) {
            this.cameraID = cameraID;
            this.cameraName = cameraName;
        }

        @Override
        public void onClick(View v) {
            if (mMainListListener != null){
                mMainListListener.onHistoryClicked(cameraID,cameraName);
            }
        }
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mIvFrame;
        private TextView tvIp,tvCameraName,tvState,tvNoFrame,tvNoInfo,tvTimeNow,tvNoCamera,tvNumber,tvISee;
        private LinearLayout llIdentify;
        private TagFlowLayout mFlowLayout;
        private RelativeLayout mRlImage,rlWarnning;
        private ImageView ivLight,ivHistory;
        private SwitchButton sbPumpState;
        public ViewHolder(View itemView) {
            super(itemView);
            mIvFrame = itemView.findViewById(R.id.ivFrame);
            tvIp = itemView.findViewById(R.id.tvIP);
            tvCameraName = itemView.findViewById(R.id.tvCameraName);
            llIdentify = itemView.findViewById(R.id.llIdentify);
            tvState = itemView.findViewById(R.id.tvState);
            mFlowLayout = itemView.findViewById(R.id.tagFlowLayout);
            mRlImage = itemView.findViewById(R.id.rlImage);
            tvNoFrame = itemView.findViewById(R.id.tvNoFrame);
            tvNoInfo = itemView.findViewById(R.id.tvNoInfo);
            tvTimeNow = itemView.findViewById(R.id.tvTimeNow);
            tvNoCamera = itemView.findViewById(R.id.tvNoCamera);
            tvNumber = itemView.findViewById(R.id.tvNumber);
            rlWarnning = itemView.findViewById(R.id.rlWarnning);
            ivLight = itemView.findViewById(R.id.ivLight);
            tvISee = itemView.findViewById(R.id.tvISee);
            sbPumpState = itemView.findViewById(R.id.sbPumpState);
            ivHistory = itemView.findViewById(R.id.ivHistory);
        }
    }

    public interface MainListListener {
        void onDetSelected(String frameID,String objDetType);
        void onImageSelected(ImageView imageView,String url);
        void onEditSelected(int position,String frameID,String text);
        void onNumberClick(String ID);
        void onSwitchButtonOnCheck(int position,boolean state,String cameraID,String cameraName);
        void onISeeClicked(int position,String cameraID);
        void onHistoryClicked(String cameraID,String cameraName);

    }
}
